package com.mbc.rmt.manager;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRResultSetDataSource;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.util.JRLoader;

import com.mbc.common.CommonSingleton;
import com.mbc.common.dao.CommonDAOFactory;
import com.mbc.common.dao.CurrencyRateDAO;
import com.mbc.common.dao.LogDAO;
import com.mbc.common.entities.CurrencyRate;
import com.mbc.common.entities.Log;
import com.mbc.common.exception.DataNotFoundException;
import com.mbc.common.manager.AbstractMgr;
import com.mbc.common.utils.KeyGenerator;
import com.mbc.rmt.dao.AgentBalanceDAO;
import com.mbc.rmt.dao.AgentCOADAO;
import com.mbc.rmt.dao.AgentDAO;
import com.mbc.rmt.dao.BranchCOADAO;
import com.mbc.rmt.dao.CashRemittanceByAgentDAO;
import com.mbc.rmt.dao.CashRemittanceByAgentDetailDAO;
import com.mbc.rmt.dao.CashRemittanceToAgentDAO;
import com.mbc.rmt.dao.CashRemittanceToAgentDetailDAO;
import com.mbc.rmt.dao.RemittanceDAOFactory;
import com.mbc.rmt.data.CashRemittanceToAgentDataList;
import com.mbc.rmt.data.CashRemittanceToAgentNDetailDataList;
import com.mbc.rmt.entities.Agent;
import com.mbc.rmt.entities.AgentBalance;
import com.mbc.rmt.entities.AgentCOA;
import com.mbc.rmt.entities.BranchCOA;
import com.mbc.rmt.entities.CashRemittanceByAgent;
import com.mbc.rmt.entities.CashRemittanceToAgent;
import com.mbc.rmt.entities.CashRemittanceToAgentDetail;
import com.mbc.rmt.entities.CashRemittanceToAgentNDetailCri;
import com.mbc.rmt.entities.CashRemittanceToAgentRpt;
import com.mbc.rmt.entities.GeneralLedger;

public class CashRemittanceToAgentMgr extends AbstractMgr {
	private CashRemittanceToAgentDAO cashRmtToAgentDAO;
	private CashRemittanceToAgentDetailDAO cashRmtToAgentDetailDAO;
	private CashRemittanceByAgentDetailDAO cashRmtByAgentDetailDAO;
	private CashRemittanceByAgentDAO cashRmtByAgentDAO;
	
	private AgentBalanceDAO agentBalanceDAO;
	private BranchCOADAO branchCOADAO;
	private CurrencyRateDAO currencyRateDAO;
	private AgentCOADAO agentCOADAO;
	private AgentDAO agentDAO;
	private LogDAO logDAO;
	/*private BankDAO bankDAO;
	private General_COADAO generalCOADAO;*/
	
	public static enum GL_STATUS { CREATE, DELETE }
	
	public CashRemittanceToAgentMgr(){}
	
	public CashRemittanceToAgentMgr(String sessionId){
		this.sessionId = sessionId;
	}
	
	public String createCashRmtToAgent(CashRemittanceByAgent confirmdata,CashRemittanceToAgent entity)throws Exception{
		openConnection();
		try {
			beginTransaction();
			
			cashRmtByAgentDAO = RemittanceDAOFactory.getCashRemittanceByAgentDAO(getConnection());
			cashRmtToAgentDAO = RemittanceDAOFactory.getCashRemittanceToAgentDAO(getConnection());
			cashRmtToAgentDetailDAO = RemittanceDAOFactory.getCashRemittanceToAgentDetailDAO(getConnection());
			logDAO = CommonDAOFactory.getLogDAO(getConnection());
			agentBalanceDAO = RemittanceDAOFactory.getAgentBalanceDAO(dbManager.getConnection());
			
			for(int i = 0; i < confirmdata.getCrByAgentDetailList().size(); i++)
				cashRmtByAgentDAO.updateStatus(confirmdata.getCrByAgentDetailList().get(i).getStatus(), confirmdata.getCrByAgentDetailList().get(i).getReceiptDetailId());
			
			cashRmtToAgentDAO = RemittanceDAOFactory.getCashRemittanceToAgentDAO(getConnection());
			entity.setReceiptID(KeyGenerator.generateKey(getConnection()));
			String voucher = RemittanceSerialMgr.getFormattedSerial(RemittanceSerialMgr.CASH_REMITTANCE_TO_AGENT, entity.getTrDate(), entity.getBranchID(), getConnection());
			entity.setReceiptNo(voucher);
			
			gotoGL(entity, GL_STATUS.CREATE);
			
			cashRmtToAgentDAO.create(entity);
			
			Log l_logentity = new Log();
			l_logentity.setTablename("CashRemittanceToAgent");
			l_logentity.setLogkey(KeyGenerator.generateKey(getConnection()));
			l_logentity.setKey(entity.getReceiptID());
			l_logentity.setUserid(entity.getCreatedBy());
			l_logentity.setLogstatus(Log.__SAVE_DATA);
			logDAO.getData(l_logentity);
			
			cashRmtToAgentDetailDAO = RemittanceDAOFactory.getCashRemittanceToAgentDetailDAO(getConnection());
			for (CashRemittanceToAgentDetail l_detail : entity.getDetails()) {
				l_detail.setCrtadetailid(KeyGenerator.generateKey(getConnection()));
				l_detail.setCrtaid(entity.getReceiptID());
				
				cashRmtToAgentDetailDAO.create(l_detail);
				
				l_logentity.setTablename("CashRemittanceToAgentDetails");
				l_logentity.setLogkey(KeyGenerator.generateKey(getConnection()));
				l_logentity.setKey(l_detail.getCrtadetailid());
				l_logentity.setLogstatus(Log.__SAVE_DATA);
				logDAO.getData(l_logentity);
			}
			
			// TODO: Update agent balance
			
			AgentBalance l_AgentBalance = new AgentBalance();
			l_AgentBalance.setAgent_id(entity.getToAgentID());
			l_AgentBalance.setCurrencyid(entity.getPaymentCurrencyID());
			l_AgentBalance = agentBalanceDAO.getInfo(l_AgentBalance);
       
			if(l_AgentBalance == null)
			{
				AgentBalance l_HAgentBalance = new AgentBalance();
				l_HAgentBalance.setAgentbalance_id(KeyGenerator.generateKey(getConnection()));
				l_HAgentBalance.setAgent_id(entity.getToAgentID());
				l_HAgentBalance.setCurrencyid(entity.getPaymentCurrencyID());
				l_HAgentBalance.setCurrentbalance(entity.getBalance());
				agentBalanceDAO.create(l_HAgentBalance);
			}
			else
			{
				l_AgentBalance.setCurrentbalance(l_AgentBalance.getCurrentbalance()+entity.getBalance());
				agentBalanceDAO.update(l_AgentBalance);
			}
			commintTransaction();
			return voucher;
		} catch (Exception e) {
			rollbackTransaction();
			throw e;
		} finally {
			closeConnection();
		}
	}
	
	public void updateCashRmtToAgent(CashRemittanceToAgent entity)throws Exception{
		openConnection();
		try {
			beginTransaction();
			
			cashRmtToAgentDAO = RemittanceDAOFactory.getCashRemittanceToAgentDAO(getConnection());
			agentBalanceDAO = RemittanceDAOFactory.getAgentBalanceDAO(dbManager.getConnection());
			cashRmtToAgentDetailDAO = RemittanceDAOFactory.getCashRemittanceToAgentDetailDAO(getConnection());
			logDAO = CommonDAOFactory.getLogDAO(getConnection());
			entity.setCreatedBy(entity.getCreatedBy());
			
			CashRemittanceToAgent l_cashremittanceToAgent = cashRmtToAgentDAO.find(entity.getReceiptID());
			ArrayList<CashRemittanceToAgentDetail> l_list = cashRmtToAgentDetailDAO.findByHeaderReceiptID(entity.getReceiptID());
			l_cashremittanceToAgent.setDetails(l_list);
			
			gotoGL(l_cashremittanceToAgent, GL_STATUS.CREATE);
			
			AgentBalance l_UAgentBalance = new AgentBalance();
			double amt=0;
			double tempamt=0;
			
			amt=entity.getBalance();
			
			CashRemittanceToAgent tempentity = new CashRemittanceToAgent();
            tempentity = cashRmtToAgentDAO.getCRToAgent(entity);
            
            tempamt = tempentity.getBalance();

        	l_UAgentBalance.setAgent_id(entity.getToAgentID());
        	l_UAgentBalance.setCurrencyid(entity.getPaymentCurrencyID());
        	l_UAgentBalance = agentBalanceDAO.getInfo(l_UAgentBalance);
        	l_UAgentBalance.setCurrentbalance((l_UAgentBalance.getCurrentbalance()-tempamt)+amt);
        	    	
        	agentBalanceDAO.update(l_UAgentBalance);
        	cashRmtToAgentDAO.updateToAgent(entity);
        	
        	Log l_logentity = new Log();
			l_logentity.setTablename("CashRemittanceToAgent");
			l_logentity.setLogkey(KeyGenerator.generateKey(getConnection()));
			l_logentity.setKey(entity.getReceiptID());
			l_logentity.setUserid(entity.getModifiedBy());
			l_logentity.setLogstatus(Log.__UPDATE_DATA);
			logDAO.getData(l_logentity);
        	
			commintTransaction();
		} catch (Exception e) {
			rollbackTransaction();
			throw e;
		} finally {
			closeConnection();
		}
	}
	
	public void deleteCashRmtToAgent(CashRemittanceToAgent entity)throws Exception{
		openConnection();
		try {
			beginTransaction();
			
			cashRmtToAgentDAO = RemittanceDAOFactory.getCashRemittanceToAgentDAO(getConnection());
			cashRmtToAgentDetailDAO = RemittanceDAOFactory.getCashRemittanceToAgentDetailDAO(getConnection());
			cashRmtByAgentDAO = RemittanceDAOFactory.getCashRemittanceByAgentDAO(getConnection());
			agentBalanceDAO = RemittanceDAOFactory.getAgentBalanceDAO(getConnection());
			logDAO = CommonDAOFactory.getLogDAO(getConnection());

			Log l_logentity = new Log();
			for(int i=0; i<entity.getDetails().size();i++){
				cashRmtByAgentDAO.updateStatus(null, entity.getDetails().get(i).getReceiptID_Detail());
				
				l_logentity.setTablename("CashRemittanceToAgentDetails");
				l_logentity.setLogkey(KeyGenerator.generateKey(getConnection()));
				l_logentity.setKey(entity.getDetails().get(i).getCrtadetailid());
				l_logentity.setUserid(entity.getModifiedBy());
				l_logentity.setLogstatus(Log.__DELETE_DATA);
				logDAO.getData(l_logentity);
			}
			
			AgentBalance l_AgentBalance = new AgentBalance();
			l_AgentBalance.setAgent_id(entity.getToAgentID());
			l_AgentBalance.setCurrencyid(entity.getPaymentCurrencyID());
			l_AgentBalance = agentBalanceDAO.getInfo(l_AgentBalance);
			
			if(l_AgentBalance != null){
				l_AgentBalance.setCurrentbalance(l_AgentBalance.getCurrentbalance()-entity.getBalance());
				agentBalanceDAO.update(l_AgentBalance);
			}
			
			gotoGL(entity, GL_STATUS.DELETE);
			
			l_logentity.setTablename("CashRemittanceToAgent");
			l_logentity.setLogkey(KeyGenerator.generateKey(getConnection()));
			l_logentity.setKey(entity.getReceiptID());
			l_logentity.setUserid(entity.getModifiedBy());
			l_logentity.setLogstatus(Log.__DELETE_DATA);
			logDAO.getData(l_logentity);
			
			cashRmtToAgentDetailDAO.deleteByHeaderReceiptID(entity.getReceiptID());
			cashRmtToAgentDAO.delete(entity.getReceiptID());
			commintTransaction();
		} catch (Exception e) {
			rollbackTransaction();
			throw e;
		} finally {
			closeConnection();
		}
	}
	
	public ArrayList<CashRemittanceToAgentDetail> loadCashRmtByAgentConfirmed(String agentCode, long paymentCurrencyID, long receivedCurrencyID)throws Exception{
		openConnection();
		try {
			cashRmtToAgentDetailDAO = RemittanceDAOFactory.getCashRemittanceToAgentDetailDAO(getConnection());
			return cashRmtToAgentDetailDAO.loadConfirmed(agentCode, paymentCurrencyID, receivedCurrencyID);
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}
	public CashRemittanceToAgentDataList findCRToAgent(CashRemittanceToAgent cri) throws Exception{
		openConnection();
		try{
			cashRmtToAgentDAO = RemittanceDAOFactory.getCashRemittanceToAgentDAO(getConnection());
			cashRmtToAgentDetailDAO = RemittanceDAOFactory.getCashRemittanceToAgentDetailDAO(getConnection());
			CashRemittanceToAgentDataList l_dataList = new CashRemittanceToAgentDataList();
			l_dataList = cashRmtToAgentDAO.find(cri);
			
			for(int i=0;i<l_dataList.getEntityList().size();i++){
				ArrayList<CashRemittanceToAgentDetail> l_detail = new ArrayList<CashRemittanceToAgentDetail>();
				l_detail = cashRmtToAgentDetailDAO.findByHeaderReceiptID(l_dataList.getEntityList().get(i).getReceiptID());
				l_dataList.getEntityList().get(i).setDetails(l_detail);
			}
			return l_dataList;
		}catch(Exception e){
			throw e;
		}finally{
			closeConnection();
		}
	}
	public CashRemittanceToAgent findCRToAgentDetail(long receiptid)throws Exception{
		openConnection();
		try{
			CashRemittanceToAgent l_agent = new CashRemittanceToAgent();
			if(receiptid != 0){
				cashRmtToAgentDetailDAO = RemittanceDAOFactory.getCashRemittanceToAgentDetailDAO(getConnection());
				l_agent.setDetails(cashRmtToAgentDetailDAO.findByHeaderReceiptID(receiptid));
			}
			return l_agent;
		}catch (Exception e){
			throw e;
		}finally{
			closeConnection();
		}
	}
	public CashRemittanceToAgentNDetailDataList findforCRToAgent(CashRemittanceToAgentNDetailCri cri)throws Exception{
		openConnection();
		try{
			cashRmtToAgentDetailDAO = RemittanceDAOFactory.getCashRemittanceToAgentDetailDAO(getConnection());
			cashRmtByAgentDetailDAO = RemittanceDAOFactory.getCashRemittanceByAgentDetailDAO(getConnection());
			return cashRmtByAgentDetailDAO.findforCRToAgent(cri);
		}catch(Exception e){
			throw e;
		}finally{
			closeConnection();
		}
	}
	public CashRemittanceToAgent find(String receiptNo)throws Exception{
		openConnection();
		try {
			cashRmtToAgentDAO = RemittanceDAOFactory.getCashRemittanceToAgentDAO(getConnection());
			CashRemittanceToAgent l_agent = new CashRemittanceToAgent();
			l_agent= cashRmtToAgentDAO.find(receiptNo);
			ArrayList<CashRemittanceToAgentDetail> l_detail = new ArrayList<CashRemittanceToAgentDetail>();
			cashRmtToAgentDetailDAO =RemittanceDAOFactory.getCashRemittanceToAgentDetailDAO(getConnection());
		    l_detail= cashRmtToAgentDetailDAO.findByHeaderReceiptID(l_agent.getReceiptID());
		    l_agent.setDetails(l_detail);
			return l_agent;
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}
	public void gotoGL(CashRemittanceToAgent l_entity, GL_STATUS status) throws Exception {
		
		currencyRateDAO = CommonDAOFactory.getCurrencyRateDAO(getConnection());
		branchCOADAO = RemittanceDAOFactory.getBranchCOADAO(getConnection());
		agentCOADAO = RemittanceDAOFactory.getAgentCOADAO(getConnection());
		agentDAO = RemittanceDAOFactory.getAgentDAO(getConnection());
	//	generalCOADAO = RemittanceDAOFactory.getGeneral_COADAO(getConnection());
				
		GeneralLedgerMgr l_glmgr = new GeneralLedgerMgr(this.sessionId);
		l_glmgr.deleteGL(l_entity.getReceiptNo() + "-TA");
		
		if(status == GL_STATUS.CREATE) {
			GeneralLedger _glHeader = new GeneralLedger();
			_glHeader.setGlKey(l_glmgr.getGLKey());
			_glHeader.setSlip_ID(l_entity.getReceiptNo() + "-TA");
			_glHeader.setSlipDate(l_entity.getReceivedDate());
			_glHeader.setSystemDate(new Date());
			_glHeader.setStatus("JH");
			_glHeader.setSrNo(1);
			_glHeader.setOffice_ID(l_entity.getBranchID());
			
			l_glmgr.createGL(_glHeader);
			
			CurrencyRate l_currencyrate = currencyRateDAO.getCurrencyRateFromSourcetoHome(l_entity.getPaymentCurrencyID(), l_entity.getBranchID());
			
			Agent l_agent = agentDAO.findById(l_entity.getToAgentID());
			if(l_agent != null) {
				AgentCOA l_agentCOA = agentCOADAO.find(l_agent.getAccountset_id());
				if(l_agentCOA!=null){
					
					GeneralLedger _glHeaderDebit = new GeneralLedger();
					_glHeaderDebit.setGlKey(l_glmgr.getGLKey());
					_glHeaderDebit.setSlip_ID(l_entity.getReceiptNo() + "-TA");
					_glHeaderDebit.setSlipDate(l_entity.getReceivedDate());
					_glHeaderDebit.setSystemDate(new Date());
					_glHeaderDebit.setStatus("JTA");
					_glHeaderDebit.setSrNo(2);
					_glHeaderDebit.setOffice_ID(l_entity.getBranchID());
					_glHeaderDebit.setCoaKey(l_agentCOA.getAccpayable());
					_glHeaderDebit.setDepartmentKey(l_agentCOA.getAccpayable_dept());
					_glHeaderDebit.setCurrencyKey(l_entity.getPaymentCurrencyID());
					_glHeaderDebit.setCurrencyRateKey(l_currencyrate.getCurrencyRateId());
					_glHeaderDebit.setExchangeRate(l_currencyrate.getRate());
					_glHeaderDebit.setSourceAmount(l_entity.getNetAmt());
					
					if(l_currencyrate.getRoper().equals("M"))
						_glHeaderDebit.setHomeAmount((l_entity.getNetAmt() * l_currencyrate.getRate()));
					else
						_glHeaderDebit.setHomeAmount((l_entity.getNetAmt() / l_currencyrate.getRate()));
					
					l_glmgr.createGL(_glHeaderDebit);
					
				}else
					throw new DataNotFoundException("");
			}
			else
				throw new DataNotFoundException("");
			
		/*	if(l_entity.getBankId()!=0){
				Bank l_bank = bankDAO.find(l_entity.getBankId());
				if(l_bank != null){
					GeneralLedger _glHeaderBank = new GeneralLedger();
					_glHeaderBank.setGlKey(KeyGenerator.generateKey(getConnection()));
					_glHeaderBank.setSlip_ID(l_entity.getReceiptNo() + "-TA");
					_glHeaderBank.setSlipDate(l_entity.getReceivedDate());
					_glHeaderBank.setSystemDate(new Date());
					_glHeaderBank.setStatus("JTA");
					_glHeaderBank.setSrNo(2);
					_glHeaderBank.setOffice_ID(l_entity.getBranchID());
					_glHeaderBank.setCoaKey(l_bank.getCoaid());
					_glHeaderBank.setDepartmentKey(l_bank.getDepartmentid());
					_glHeaderBank.setCurrencyKey(l_entity.getPaymentCurrencyID());
					_glHeaderBank.setCurrencyRateKey(l_currencyrate.getCurrencyRateId());
					_glHeaderBank.setExchangeRate(l_currencyrate.getRate());
					_glHeaderBank.setSourceAmount(-l_entity.getPayment());
					
					if(l_currencyrate.getRoper().equals("M"))
						_glHeaderBank.setHomeAmount(-(l_entity.getPayment() * l_currencyrate.getRate()));
					else
						_glHeaderBank.setHomeAmount(-(l_entity.getPayment() / l_currencyrate.getRate()));
					
					l_glmgr.createGL(_glHeaderBank);
				}
			}else{
				General_COA l_generalCOA = generalCOADAO.find(l_entity.getBranchID());
				
				if(l_generalCOA == null)
					throw new DataNotFoundException("Plz fill general coa");
				
				GeneralLedger _glHeaderGeneral = new GeneralLedger();
				_glHeaderGeneral.setGlKey(KeyGenerator.generateKey(getConnection()));
				_glHeaderGeneral.setSlip_ID(l_entity.getReceiptNo() + "-TA");
				_glHeaderGeneral.setSlipDate(l_entity.getReceivedDate());
				_glHeaderGeneral.setSystemDate(new Date());
				_glHeaderGeneral.setStatus("JTA");
				_glHeaderGeneral.setSrNo(2);
				_glHeaderGeneral.setOffice_ID(l_entity.getBranchID());
				_glHeaderGeneral.setCoaKey(l_generalCOA.getCashAccountID());
				_glHeaderGeneral.setDepartmentKey(l_generalCOA.getCashDepartmentID());
				_glHeaderGeneral.setCurrencyKey(l_entity.getPaymentCurrencyID());
				_glHeaderGeneral.setCurrencyRateKey(l_currencyrate.getCurrencyRateId());
				_glHeaderGeneral.setExchangeRate(l_currencyrate.getRate());
				_glHeaderGeneral.setSourceAmount(-l_entity.getPayment());
				
				if(l_currencyrate.getRoper().equals("M"))
					_glHeaderGeneral.setHomeAmount(-(l_entity.getPayment() * l_currencyrate.getRate()));
				else
					_glHeaderGeneral.setHomeAmount(-(l_entity.getPayment() / l_currencyrate.getRate()));
				
				l_glmgr.createGL(_glHeaderGeneral);
			}*/
			
			for(int i = 0; i < l_entity.getDetails().size(); i++) {
				
				CurrencyRate l_currencyrate1 = currencyRateDAO.getCurrencyRateFromSourcetoHome(l_entity.getReceivedCurrencyID(), l_entity.getBranchID());
				
				if(l_entity.getDetails().get(i).getFromBranchID()!=0) {
					BranchCOA l_branchCOA = branchCOADAO.findByHostBranchID(l_entity.getBranchID(),l_entity.getDetails().get(i).getFromBranchID());
				
					if(l_branchCOA == null)
						throw new DataNotFoundException("Plz fill BranchCOA",BranchCOA.class);	
					
					GeneralLedger _glDebit = new GeneralLedger();
					_glDebit.setGlKey(l_glmgr.getGLKey());
					_glDebit.setSlip_ID(l_entity.getReceiptNo() + "-TA");
					_glDebit.setSlipDate(l_entity.getReceivedDate());
					_glDebit.setSystemDate(new Date());
					_glDebit.setCoaKey(l_branchCOA.getCurrentAccountID());
					_glDebit.setDepartmentKey(l_branchCOA.getDepartmentID());
					_glDebit.setCurrencyKey(l_entity.getReceivedCurrencyID());
					_glDebit.setCurrencyRateKey(l_currencyrate1.getCurrencyRateId());
					_glDebit.setExchangeRate(l_currencyrate1.getRate());
					_glDebit.setSourceAmount(-l_entity.getDetails().get(i).getPaymentAmount());
					
					if(l_currencyrate1.getRoper().equals("M"))
						_glDebit.setHomeAmount(-(l_entity.getDetails().get(i).getPaymentAmount() * l_currencyrate1.getRate()));
					else
						_glDebit.setHomeAmount(-(l_entity.getDetails().get(i).getPaymentAmount() / l_currencyrate1.getRate()));
					
					_glDebit.setStatus("JTA");
					_glDebit.setSrNo((i + 3));
					_glDebit.setOffice_ID(l_entity.getBranchID());
					
					l_glmgr.createGL(_glDebit);
				
				}else if(l_entity.getDetails().get(i).getFromAgentID()!=0){
					
					Agent l_agent1 = agentDAO.findById(l_entity.getDetails().get(i).getFromAgentID());
					if(l_agent1 != null) {
						AgentCOA l_agentCOA = agentCOADAO.find(l_agent1.getAccountset_id());
						
						if(l_agentCOA==null)
							throw new DataNotFoundException("Please fill AgentCOA", AgentCOA.class);
						
						GeneralLedger _glDetailCredit = new GeneralLedger();
						_glDetailCredit.setGlKey(l_glmgr.getGLKey());
						_glDetailCredit.setSlip_ID(l_entity.getReceiptNo() + "-TA");
						_glDetailCredit.setSlipDate(l_entity.getReceivedDate());
						_glDetailCredit.setSystemDate(new Date());
						_glDetailCredit.setStatus("JTA");
						_glDetailCredit.setSrNo(2);
						_glDetailCredit.setOffice_ID(l_entity.getBranchID());
						_glDetailCredit.setCoaKey(l_agentCOA.getAccpayable());
						_glDetailCredit.setDepartmentKey(l_agentCOA.getAccpayable_dept());
						_glDetailCredit.setCurrencyKey(l_entity.getReceivedCurrencyID());
						_glDetailCredit.setCurrencyRateKey(l_currencyrate1.getCurrencyRateId());
						_glDetailCredit.setExchangeRate(l_currencyrate1.getRate());
						_glDetailCredit.setSourceAmount(-l_entity.getDetails().get(i).getPaymentAmount());
						
						if(l_currencyrate1.getRoper().equals("M"))
							_glDetailCredit.setHomeAmount(-(l_entity.getDetails().get(i).getPaymentAmount() * l_currencyrate1.getRate()));
						else
							_glDetailCredit.setHomeAmount(-(l_entity.getDetails().get(i).getPaymentAmount() / l_currencyrate1.getRate()));
						
						l_glmgr.createGL(_glDetailCredit);
					}
					else
						throw new DataNotFoundException("");
					
				}
			}
		}
	}
	//Report
	public JasperPrint prepareCashRemittanceToAgentReport(CashRemittanceToAgentRpt entity)throws Exception{
		openConnection();
		JasperPrint l_jsPrint = null;
		try{
			cashRmtToAgentDAO = RemittanceDAOFactory.getCashRemittanceToAgentDAO(getConnection());
			JRResultSetDataSource l_jrRS = new JRResultSetDataSource(cashRmtToAgentDAO.prepareCashRemittanceToAgentReport(entity));
			JasperReport l_jsRpt = null;
			if(entity.getStatus().equalsIgnoreCase("summary"))
				l_jsRpt = (JasperReport) JRLoader.loadObjectFromFile(CommonSingleton.ABSOLUTE_PATH+"reports/CashRemittanceToAgentSummary.jasper");
			else
				l_jsRpt = (JasperReport) JRLoader.loadObjectFromFile(CommonSingleton.ABSOLUTE_PATH+"reports/CashRemittanceToAgentDetail.jasper");
			HashMap<String, Object> l_params = new HashMap<String, Object>();
			l_params.put("company", "mbc");
			l_jsPrint = JasperFillManager.fillReport(l_jsRpt, l_params, l_jrRS);
		}catch (JRException je){
			throw je;
		}catch (Exception e){
			throw e;
		}finally{
			closeConnection();
		}
		return l_jsPrint;
	}
	
	public int getTotalReceipt(long branchId, long hobranchid)throws Exception{
		openConnection();
		try {
			cashRmtToAgentDAO = RemittanceDAOFactory.getCashRemittanceToAgentDAO(getConnection());
			return cashRmtToAgentDAO.getTotalReceipt(branchId, hobranchid);
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}
}
